package com.example.securitydemo.authentication;

import com.example.securitydemo.entities.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

public class MyUserDetails implements UserDetails {
    private User user;
    private Collection<? extends GrantedAuthority> authorities;

    public MyUserDetails(User user, Collection<? extends GrantedAuthority> authorities) {
        this.user = user;
        this.authorities = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.user.getPassword();
    }

    @Override
    public String getUsername() {
        return this.user.getUsername();
    }

    //是否已过期。
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //用户锁定还是解锁
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //用户的凭据（密码）是否已过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //指示启用还是禁用用户
    @Override
    public boolean isEnabled() {
        return this.user.getStatus() == User.STATUS_VALID;
    }
}
